#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>


int main()
{
    //打开文件
    int fd = open("bite", O_CREAT | O_WRONLY | O_TRUNC, 0664);
    if (fd < 0)
    {
        perror("open fail");
        return 1;
    }

    //向文件写入内容
    const char* content = "i like linux!";
    int ret_write = write(fd, content, strlen(content));
    if (ret_write < 0)
    {
        perror("write fail");
        return 2;
    }

    close(fd);

    fd = open("bite", O_RDONLY);

    //改变文件流位置
    if(lseek(fd, 0 , SEEK_SET) == -1)
    {
        perror("lseek fail");
        return 3;
    }

    char buffer[1024];
    int n = read(fd, buffer, sizeof(buffer) - 1);
    if (n > 0)
    {
        buffer[n] = 0;
        printf("%s\n", buffer);
    }

    close(fd);
    return 0;
}